<?php
include_once '../includes/common.php';
$proid = intval($_GET['proid']);
$url = daddslashes($_GET['url']);
$authcode = daddslashes($_GET['authcode']);
$ver = daddslashes($_GET['ver']);
$program = $DB->get_row("SELECT * FROM authguao_program WHERE id='$proid' limit 1");
$siterow = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='$url' and authcode='$authcode' limit 1");
if(!$siterow){
    $url = "*.".get_host($url);
    $siterow = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='*.".get_host($url)."' and authcode='$authcode' limit 1");
}
$sql = array();
if (!$proid) {
    $result=array("code"=>-1,"msg"=>'程序识别码错误！');
} else if (strlen($authcode)!=32 || !$authcode) {
    $result=array("code"=>-1,"msg"=>'授权码错误！');
} else if ($authcode != $siterow['authcode']) {
    $result=array("code"=>-1,"msg"=>'授权KEY验证失败，请检查配置！');
} else if ($url=='localhost' || $url=='127.0.0.1') {
    $result=array("code"=>-1,"msg"=>'本地地址无法提交！');
} else if (!$program) {
    $result=array('code'=>-1,'msg'=>'授权平台不存在该程序！');
} else {
    if ($conf['active_program'] == 1) {
        if ($program['active'] == 0) {
            $result=array('code'=>-1,'msg'=>'授权平台已停止运营该程序！');
        }
    } else {
        $notauthtips = !empty($program['notauth_msg']) ? $program['notauth_msg'] : "检查到您的网站未授权,暂时无法使用我们的程序!";
        $errtips = !empty($program['errauth_msg']) ? $program['errauth_msg'] : "检查到您的网站使用异常,已被封禁,暂时无法使用我们的程序!";
        $endtips = !empty($program['endauth_msg']) ? $program['endauth_msg'] : "您的网站已到期,请联系客服续费!";
        if ($ver) {
            $param = base64_encode(authcode($proid."|".$ver."|".$url."|".$authcode."|".(TIMESTAMP+600),'ENCODE','authguaos!!'));
            $download = $authurl.'api/download.php?update=true&param='.$param.'&rand='.rand(100000,999999);
            if ($program['update'] == 1) {
                if($ver >= $program['version']) {
                    $code=0;
                    $msg='<font color="green">您使用的程序：'.$program['name'].'，已是最新版本！</font><br/>当前版本：'.$program['ver'].' (Build '.$program['version'].')';
                } else {
                    $code=1;
                    $msg='<font color="red">您使用的程序：'.$program['name'].'，发现新版本(～￣▽￣)～！</font> 最新版本：'.$program['ver'].' (Build '.$program['version'].')';
                }
            } else if ($siterow['active'] == 0) {
                $code=0;
                $msg='<font color="blue">您使用的程序：'.$program['name'].'的授权，已被封禁，无法更新！</font>';
            } else {
                $code=0;
                $msg='<font color="blue">更新服务器正在维护，请稍后访问！</font>';
            }
        }
        if ($program['switch'] == 0 || $proid && $authcode && checkauth($proid,$url,$authcode) || $url=='localhost' || $url=='127.0.0.1') {
            if (!empty($ver)) {
                $result=array('code'=>$code,'msg'=>$msg,'uplog'=>$program['uplog'],'file'=>$download,'sql'=>$sql);
            } else {
                $result=array('code'=>1,'authcode'=>$authcode);
            }
        } else {
            if ($siterow['active'] == 0) {
                $result=array('code'=>-1,'msg'=>$errtips);
            } else if ($siterow['endtime'] < $date) {
                $result=array('code'=>-1,'msg'=>$endtips);
            } else {
                if ($program['block'] == 1) {
                    $result=array('code'=>2,'msg'=>$notauthtips);
                } else if ($program['block'] == 2) {
                    $result=array('code'=>3,'msg'=>$notauthtips);
                } else if ($program['block'] == 3) {
                    $result=array('code'=>4,'msg'=>$notauthtips);
                } else {
                    $result=array('code'=>-1,'msg'=>$notauthtips);
                }
            }
        }
    }
}
echo json_encode($result);
$DB->close();
?>